package com.songlanyun.modules.shop.controller.api;

import java.util.List;
import java.util.Map;


import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.shop.entity.ShopEntity;
import com.songlanyun.modules.shop.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import com.songlanyun.common.utils.R;


/**
 * 商家表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-03 14:34:03
 */
@RestController
@RequestMapping("api/yxt/shop")
public class ShopApiController extends AbstractApiController {
    @Autowired
    private ShopService shopService;


    /**
     * 进入页面显示默认商家店铺
     */
    @GetMapping("/getShop")
    public R getShop() {

        Long shopId = null;
        Long userId = getUserId(false);
        ShopEntity shop = null;

        if (userId == 0) {
            //1、首次登录未注册显示平台店铺
            shop = shopService.selectPlatformShop();
        } else {
            //如果已经登录，就返回该会员隶属商铺
            //2、首次登录已注册显示上级店铺
            try {
                shopId = getMember().getSubjectionShopId();
                shop = shopService.getApiShopInfo(shopId, false);
            } catch (Exception e) {//找不到隶属店铺，则返回平台店铺id
                shop = shopService.selectPlatformShop();
            }
        }

        return R.ok().put("data", shop);
    }

    /**
     * 查询当前会员的商家信息
     */
    @GetMapping("/my-shop")
    public R myShop(@RequestParam(value = "union_business", required = false) Boolean unionBusiness) {
        ShopEntity shop = shopService.getApiShopInfo(getUserId(true), ObjectUtil.isNull(unionBusiness) ? false : unionBusiness);
        return R.ok().put("data", shop);
    }

    /**
     * 扫商品码进入小程序显示默认店铺
     */
    @GetMapping("/getShop/{shopId}")
    public R getShop(@PathVariable("shopId") Long shopId) {
        //扫描分享码进来显示分享码中商品的店铺 -- 将商铺id显示到路由中传过来
        ShopEntity shop = shopService.getById(shopId);
        return R.ok().put("data", shop);
    }

    /**
     * 获取指定店铺信息
     */
    @GetMapping("/info/{shopId}")
    public R info(@PathVariable("shopId") Long shopId) {
        ShopEntity shop = shopService.getById(shopId);
        return R.ok().put("data", shop);
    }

    /**
     * 根据经纬度查询附近的店铺，按远近排序
     */
    @GetMapping("/nearbyShop")
    public R nearbyShop(@RequestParam Map<String, Object> params) {
        List<ShopEntity> list = shopService.nearbyShop(params);

        return R.ok().put("list", list);
    }

    /**
     * 根据城市查询所有的店铺
     */
    @GetMapping("/all-shop-city/{city}")
    public R allShopCity(@PathVariable("city") String city) {
        List<ShopEntity> list = shopService.allShopCity(city, false);

        return R.ok().put("list", list);
    }

    /**
     * 所有门店
     *
     * @return
     */
    @GetMapping("/all-shop")
    public R allShop() {
        List<ShopEntity> list = shopService.allShop(false);
        return R.ok().put("list", list);
    }


}
